package stack;



/**
 * @Program: dataOJ->ArrayStackDemo
 * @Description: 数组实现栈
 * @Author: Tansty
 * @Date: 2022-02-09 23:09
 * @Version： 1.0
 **/

public class ArrayStackDemo {
    private int top = -1;
    private int[] array = null;
    private int maxSize = 0;

    public ArrayStackDemo(int maxSize) {
        this.maxSize = maxSize;
        array = new int[maxSize];
    }

    public boolean isNull() {
        return this.top == -1;
    }

    public boolean isFull() {
        return this.top >= this.maxSize - 1;
    }

    public void list() {
        for (int i = 0; i <= top; i++) {
            System.out.println(array[i]);
        }
    }

    public void push(int value) {
        if (isFull()) {
            throw new RuntimeException("栈已满");
        }
        array[++top] = value;
    }

    public int pop() {
        if (isNull()) {
            throw new RuntimeException("栈为空");
        }
        return array[top--];
    }

    public static void main(String[] args) {
        ArrayStackDemo stackDemo = new ArrayStackDemo(5);
        stackDemo.push(1);
        stackDemo.push(2);
        stackDemo.push(13);
        stackDemo.push(13);
        stackDemo.push(13);
        stackDemo.list();
        System.out.println(stackDemo.pop());
        System.out.println(stackDemo.pop());
        System.out.println(stackDemo.pop());
        System.out.println(stackDemo.pop());
        stackDemo.list();

    }
}

